package dataapi

import (
	"iot-base/common/notify"
	"iot-base/common/webapi"
	"strconv"

	"fmt"
	"iot-base/common/cache"
	_ "iot-base/common/config"
	"iot-base/common/dbconn"
	rc "iot-base/common/redis"

	"github.com/gin-gonic/gin"
	"iot-base/common/ctrltype"
)

//InitDataAPI 初始化api
func InitDataAPI(api *gin.RouterGroup) {
	initVideoAddr(api)
	dataAPI := api.Group("data")
	{
		dataAPI.GET("/sview/:sid", func(c *gin.Context) {
			sid := c.Param("sid")
			if sid == "" {
				webapi.SendError("staion id is empty", webapi.ParamsError, c)
				return
			}
			views, err := cache.GetStationView(sid)
			if err != nil {
				webapi.SendError(" station id "+sid+" error:"+err.Error(), webapi.DBError, c)
				return
			}
			webapi.SendResult(views, nil, c)
		})

		dataAPI.GET("/view/:tid", func(c *gin.Context) {
			tid := c.Param("tid")
			kind := c.Query("kind")
			pid := c.Query("pid")
			subid := c.Query("subid")
			pindex := c.Query("index")
			if pid == "" && subid == "" && pindex == "" && kind == "" {
				view, err := cache.GetThingView(tid)
				webapi.SendResult(view, err, c)
				return
			} else if pid != "" {
				var index = 0
				if pindex != "" {
					index, _ = strconv.Atoi(pindex)
				}
				if kind == "hour" {
					datas, err := cache.GetIntegralHourDatas(tid, pid, subid, index)
					webapi.SendResult(datas, err, c)
					return

				} else {
					datas, err := cache.GetLastValues(tid, pid, subid, index)
					webapi.SendResult(datas, err, c)
					return
				}
			}

		})
		dataAPI.GET("/real", func(c *gin.Context) {
			tid := c.Query("tid")
			pid := c.Query("pid")
			sid := c.Query("subid")
			sindex := c.Query("index")
			if sindex == "" {
				sindex = "-1"
			}
			if pid == "" {
				pid = "all"
			}
			if sid == "" {
				sid = "all"
			}
			index, _ := strconv.ParseInt(sindex, 10, 64)
			dataID := cache.MakeDataID(tid, pid, sid, int(index))
			result, err := rc.GetListByPattern(dataID)
			if err == nil {
				webapi.SendResult(result, nil, c)
			} else {
				webapi.SendError(err.Error(), webapi.RedisError, c)
			}

		})
		dataAPI.GET("/force/:tid", func(c *gin.Context) {
			tid := c.Param("tid")
			if tid != "" {
				notify.PubMsg(false, "forceData", tid)
				webapi.SendOK(c)
			} else {
				webapi.SendError("param missed", webapi.ParamsError, c)
			}
		})
		dataAPI.GET("/edgeForce/:tid", func(c *gin.Context) {
			tid := c.Param("tid")
			if tid != "" {
				notify.CtrlThing(tid, "queryData", 10, nil, func(pResult *ctrltype.CtrlResult) {
					fmt.Println("********************query begin----------------", pResult)
					if pResult != nil && pResult.Result != nil {
						//[]*params.ValueInRedis
						//pResult.Result
						datas := pResult.Result["datas"]
						fmt.Println("********************query----------------", datas)
						if datas != nil {
							idatas, oki := datas.([]map[string]interface{})
							if oki && idatas != nil {
								for _, data := range idatas {
									if data != nil {
										//pRData := new(propertytype.Value)
										notify.RawDataMap(data)
									}
								}
							}
						}
					}
				}, false)
				webapi.SendOK(c)
			} else {
				webapi.SendError("param missed", webapi.ParamsError, c)
			}
		})
		dataAPI.GET("/histroy", func(c *gin.Context) {
			tid := c.Query("tid")
			pid := c.Query("pid")
			sid := c.Query("subid")
			sindex := c.Query("index")
			sBegin := c.Query("begintime")
			sEnd := c.Query("endtime")
			sql := "select pick_time as picktime,type,value,pn,tname,sname,mindex as pi,tid,pid,sid from realdata where 1=1"
			if sindex != "-1" {
				_, err := strconv.ParseInt(sindex, 10, 64)
				if err == nil {
					sql = sql + " and mindex=" + sindex
				}

			}
			if tid != "" {
				sql = sql + " and tid='" + tid + "'"
			}
			if pid != "" {
				sql = sql + " and pid='" + pid + "'"
			}
			if sid != "" {
				sql = sql + " and sid='" + sid + "'"
			}
			if sBegin != "" {
				sql = sql + " and pick_time >='" + sBegin + "'"
			}
			if sEnd != "" {
				sql = sql + " and pick_time <='" + sEnd + "'"
			}
			sql = sql + " order by pick_time"
			var result []map[string]interface{}
			err := dbconn.DBConnection.Raw(sql).Find(&result).Error
			if err == nil {
				webapi.SendResult(result, nil, c)
			} else {
				webapi.SendError(err.Error(), webapi.RedisError, c)
			}

		})

	}
}
